package com.eipc;

import org.androidpn.sdk.Constants;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TabHost;
import com.eipc.model.ImportNotice;
import com.eipc.model.SerInfoResponse;
import com.eipc.util.DBHelper;
import com.eipc.util.HttpManager;
import com.eipc.util.JsonParse;
import com.eipc.util.MyLog;

public class WorkReportActivity extends CommonTabActivity {
	public final static int TYPE = DBHelper.APP_TYPE_WORK_REPORT;
	private PollingTask mTask;
	private int mType;
	private boolean isFi = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.import_notice);
        initView();
        mType = getIntent().getIntExtra("type",0);
        mTabHost.setCurrentTab(mType);
	}
    
    protected void initView(){
    	super.initView();
    	mBar.setMiddleText(R.string.work_report);
    	mBar.setOnRightClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
				mTask = new PollingTask();
				mTask.execute();
			}
		});
    	mTabHost = getTabHost();
    	mTabHost.setFadingEdgeLength(10);
    	Intent i = new Intent(this,ReceiveActivity.class);
    	i.putExtra("type", TYPE);
    	mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(buildLabel(R.string.import_notice_receive)).setContent(i));
    	if(mPerson==null|| !mPerson.isLeader.equals(DBHelper.STATUS.LEADER_HIGHT)){
	    	i = new Intent(this,SendActivity.class);
	    	i.putExtra("type", TYPE);
	    	i.putExtra("leader", mPerson.isLeader);
	    	mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(buildLabel(R.string.import_notice_send)).setContent(i));
	    	i = new Intent(this,ReceivedActivity.class);
	    	i.putExtra("type", TYPE);
	    	mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator(buildLabel(R.string.import_notice_received)).setContent(i));
    	}
    }
    
    private TabHost mTabHost;
    
    class PollingTask extends CommonTask<Void>{
		public PollingTask(){
			super(WorkReportActivity.this);
		}
		private String mResult;
		@Override
		protected Void doInBackground(Void... params) {
			try {
				SharedPreferences sdkPreferences = getSharedPreferences(Constants.SDK_PREFERENCES, Context.MODE_PRIVATE);
				String mobileID = sdkPreferences.getString(Constants.XMPP_USERNAME,"");
				String maxID    = sdkPreferences.getString(Constants.MaxId,"0");
				String ids = mDB.getIDs();
				mResult = HttpManager.polling(getString(R.string.url_polling),Constants.TTYPE_NOTICE,maxID,ids,mobileID,"0");
				Log.e("------------refesh notices-------------",String.valueOf(mResult));
				if(mResult!=null && !"".equals(mResult)){
					SerInfoResponse resp = JsonParse.parseSerInfoResponse(mResult,Constants.LOAD_INPORTNOTICE);
					if(resp!=null&&resp.success&&resp.os!=null&&resp.os.length>0){
						if(resp.id!=null&&!"".equals(resp.id)){
							mDB.updateMaxId(resp.id);
							try{mDB.insertImportNotice((ImportNotice[])resp.os,resp.id);}catch(Exception e){}
						}
					}else{
							Log.e(this.getClass().getName()+" polling failure : ",resp.toString());
					}
				}else{
					Log.e(this.getClass().getName()+" polling failure : ","mResult is null");
				}
				
			} catch (Exception e) {
				MyLog.error(FetchConsultService.class,"",e);
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result){
			super.onPostExecute(result);
				Intent i = new Intent(WorkReportActivity.this,WorkReportActivity.class);
				startActivity(i);
				finish();
		}
		
	}
    
    @Override
    protected void updateView(){
    	super.updateView();
    	isFi = false;
    	Intent i = new Intent(WorkReportActivity.this,WorkReportActivity.class);
		startActivity(i);
		finish();
    }
}
